package com.l024.easyandroid;

import android.widget.ImageView;

import com.apkfuns.logutils.LogUtils;
import com.l024.easyandroid.api.service.WananService;
import com.l024.easyandroid.databinding.ActivityMainBinding;
import com.l024.easyandroid.fragment.HomeFragment;
import com.l024.easyandroidlib.base.BaseActivity;
import com.l024.easyandroidlib.cache.DbCache;
import com.l024.easyandroidlib.cache.MMKVCache;
import com.l024.easyandroidlib.dialog.manager.DialogManager;
import com.l024.easyandroidlib.dialog.view.DialogView;
import com.l024.easyandroidlib.helper.GlideHelper;
import com.l024.easyandroidlib.http.HttpManager;
import com.l024.easyandroidlib.http.observer.BaseObserver;
import com.l024.easyandroidlib.utils.FileUtil;
import com.l024.easyandroidlib.utils.RxJavaUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import me.yokeyword.fragmentation.ISupportFragment;

public class MainActivity extends BaseActivity<ActivityMainBinding> {


    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
    }

    @Override
    public void initData() {
        cacheTest();
        netTest();
        fileTest();
        logTest();
    }

    @Override
    public void initEvent() {

    }


    //缓存测试
    private void cacheTest(){

        MMKVCache.getInstance().save("key","1212");
        LogUtils.i("緩存数据",MMKVCache.getInstance().getStringValue("key4545"));
        //数据库缓存
        DbCache.getInstance().save("xxxx","545",2000);
        String key = DbCache.getInstance().getByKey("xxxx");
        LogUtils.e("读取的key:"+key);
        //删除所有
        DbCache.getInstance().delAll();
    }
    //网络测试
    private void netTest(){
        //网络请求 可以继承BaseObserver实现各种业务网络请求逻辑
        HttpManager.getInstance().create(WananService.class).getArticleList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    protected String setTag() {
                        return "list";
                    }

                    @Override
                    public void doOnSubscribe(Disposable d) {
                        LogUtils.e("开始请求");
                        showLoading();
                    }

                    @Override
                    public void doOnError(String errorMsg) {
                        LogUtils.e("请求失败："+errorMsg);
                    }

                    @Override
                    public void doOnNext(String s) {
                        LogUtils.e("请求成功"+s);
                    }

                    @Override
                    public void doOnCompleted() {
                        LogUtils.e("请求结束");
//                        loadingDialog.hide();
                        hideLoading();
                    }
                });

        //切换域名
        RxJavaUtil.commonObservable(HttpManager.getInstance().create("baseUrl",WananService.class).getArticleList())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    protected String setTag() {
                        return null;
                    }

                    @Override
                    public void doOnSubscribe(Disposable d) {

                    }

                    @Override
                    public void doOnError(String errorMsg) {

                    }

                    @Override
                    public void doOnNext(String s) {

                    }

                    @Override
                    public void doOnCompleted() {

                    }
                });
    }

    //文件流读取
    private void fileTest(){
        FileUtil.saveStringToFile("/sdcard/apk","测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据","1.txt",false);
        String s = FileUtil.readFile("/sdcard/apk/1.txt");
        LogUtils.e("文件读取:"+s);
    }

    //日志测试
    private void logTest(){
        LogUtils.d("打印数据");
        LogUtils.e(null);
        LogUtils.tag("log").d("打印数据");
        LogUtils.wtf("写入文件数据");
    }

    //弹窗
    private void dialogTest(){
        DialogView dialogView  = DialogManager.getInstance().initView(this, R.layout.dialog_test);
        //显示
        DialogManager.getInstance().show(dialogView);
        //隐藏
        DialogManager.getInstance().hide(dialogView);
    }

    //加载图片
    private void loadImg(){
        ImageView imageView = new ImageView(this);
        //加载图片
        GlideHelper.loadUrl(this,"http://xxxx.png",imageView);
    }

    //fragment的使用
    private void fragmentTest(){
        //加载fragment
        loadRootFragment(R.id.fl_box, HomeFragment.getInstance());
        //切换跳转
        start(HomeFragment.getInstance());
        //启动模式
        start(HomeFragment.getInstance(), ISupportFragment.STANDARD);
    }

}